$(function() {

    //获取验证码
    $(document).on('click','#checkCode' ,function(){
        senderValid();
    });

    //手机登录
    $(document).on('click','.mobileSubmit' ,function(){
        var mobilePhone = $('#mobilePhone').val();
        var filter = /^1\d{10}$/;
        if (mobilePhone == "") {
            Prompt.show("手机号不能为空!",'提示',function(){});
            return false;
        }
        if (filter.test(mobilePhone) == false) {
            Prompt.show("手机号错误!",'提示',function(){});
            return false;
        }
        var mobileValid = $('#yanzheng').val();
        if (mobileValid == "") {
            Prompt.show("验证码不能为空!",'提示',function(){});
            return false;
        }
        var data = {
            phone:mobilePhone,
            mobileValid:mobileValid
        }
        app.post(app.url.api_base+"login/mobileAndValidLogin.do",data,
            function(req) { 
                 if(req.code==0){
                    app.addItem('userName',req.data.userName);
                    app.addItem('userId',req.data.id);
                    app.addItem('userToken',req.data.userToken);
                    app.go('home.html');
                  }else{
                    Prompt.show(req.message)
                  }
            },false);
    });

    //手机登录
    $(document).on('click','.accountSubmit' ,function(){
        var userName = $('#userName').val();
        if (userName == "") {
            Prompt.show("账号不能为空!",'提示',function(){});
            return false;
        }
        var password = $('#NewPass').val();
        if (password == "") {
            Prompt.show("密码不能为空!",'提示',function(){});
            return false;
        }
        var hash = $.md5(password);
        var data = {
            loginName:userName,
            password:hash
        }
        app.post(app.url.api_base+"login/userLogin.do",data,
            function(req) { 
                 if(req.code==0){
                    app.addItem('userName',req.data.userName);
                    app.addItem('userId',req.data.id);
                    app.addItem('userToken',req.data.userToken);
                    app.go('home.html');
                  }else{
                    Prompt.show(req.message)
                  }
            },false);
    });

    //发送验证码
    function senderValid() {
        var name = $('#name').val().trim();
        var IdCard = $('#IdCard').val().trim();
        var mobilePhone = $('#mobilePhone').val().trim();
        var PassWorld = $('#PassWorld').val().trim();
        var filter = /^1\d{10}$/;

        if (name == "") {
            Prompt.show("姓名不能为空!",'提示',function(){});
            return false;
        }
        if (IdCard == "") {
            Prompt.show("身份证号不能为空!",'提示',function(){});
            return false;
        }
        if (mobilePhone == "") {
            Prompt.show("手机号不能为空!",'提示',function(){});
            return false;
        }
        if (filter.test(mobilePhone) == false) {
            Prompt.show("手机号错误!",'提示',function(){});
            return false;
        }
        if (PassWorld == "") {
            Prompt.show("密码号不能为空!",'提示',function(){});
            return false;
        }
        app.post( app.url.api_base+"login/sendMobileValidForLogin.do",{phone:mobilePhone},
            function(req) { 
                Prompt.show(req.message);
            },true);
        setButtonName();
    }

    //验证码计时
    function setButtonName() {
        var num = 60
        clearInterval(timer);
        var timer = setInterval(function() {
            num = num - 1;
            $('#checkCode').html('请等待(' + num + ')');
            $(document).off('click','#checkCode' );
            if (num == 0) {
                clearInterval(timer);
                $('#checkCode').html("获取验证码");
                //获取验证码
                $(document).on('click','#checkCode' ,function(){
                    senderValid();
                });
            }
        }, 1000)

    }


})

